package org.coco.mybatis.session;

import org.coco.mybatis.mapping.BoundSql;
import org.coco.mybatis.mapping.ResultMap;
import org.coco.mybatis.mapping.ResultMapping;
import org.coco.util.SetterValueUtil;
import org.coco.util.ValidationUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

public class ResultParse<E> {

    public  List<E> parseForMany(ResultSet resultSet, BoundSql boundSql) {
        List resultList = new ArrayList();
        ResultMap resultMap = null;
        try {
            resultMap = boundSql.getResultMap();
            //获取返回对象类型
            Class<?> clazz = boundSql.getClazz();
            ResultSetMetaData metaData = resultSet.getMetaData();
            //得到sql查询出所有的列
            int columnCount = metaData.getColumnCount();
            int var0 = columnCount;
            List<Object> columns = new ArrayList<>();
            String columnName = null;
            for(int i = 0; i < var0; i ++) {
                columnName = metaData.getColumnName(i+1);
                columns.add(columnName);
            }
            Class newInstance = null;
            Object instance = null;
            while (resultSet.next()) {
                int var1 = columns.size();
                    if(resultMap == null) {
                        //说明用户没有在xml配置ResultMap
                        instance = clazz.newInstance();
                        //获取该对象所有的属性
                        Field[] declaredFields = clazz.getDeclaredFields();
                        int var2 = declaredFields.length;
                        boolean fieldIsExist = false;
                        for(int t = 0; t < var1; t++) {
                            //获取从数据中查询到的列名
                            Object column = columns.get(t);
                            //根据列名获取从数据库查询到该列的数据
                            Object object = resultSet.getObject(String.valueOf(column));
                            //开始遍历该对象的属性
                            for(int y = 0; y < var2; y ++) {
                                Field filed = declaredFields[y];
                                String filedName = filed.getName();
                                if(column.equals(filedName)) {
                                    //获取该属性的类型
                                    Class<?> filedType = filed.getType();
                                    SetterValueUtil.setter(instance,filedName,object,filedType);
                                    fieldIsExist = true;
                                    continue;
                                }
                            }
                            if(!fieldIsExist) {
                                throw new RuntimeException("column "+column +" is not exist ");
                            }
                        }
                    }else {
                        newInstance = Class.forName(resultMap.getType());
                        instance = newInstance.newInstance();
                        for(int t = 0; t < var1; t++) {
                            //获取从数据中查询到的列名
                            Object column = columns.get(t);
                            //说明用户配置了ResultMap
                            List<ResultMapping> resultMappingList = resultMap.getResultMappingList();
                            String property = null;
                            int var2 = resultMappingList.size();
                            boolean flag = true;
                            ResultMapping resultMapping = null;
                            for(int k = 0; k < var2 && flag; k ++) {
                                resultMapping = resultMappingList.get(k);
                                property = resultMapping.getProperty();
                                if(property.equals(columns.get(t))) {
                                    //根据列名获取从数据库查询到该列的数据
                                    Object object = resultSet.getObject(String.valueOf(column));
                                    //获取该字段的类型
                                    Class<?> javaTypeClass = resultMapping.getJavaTypeClass();
                                    String columnParam = resultMapping.getColumn();
                                    SetterValueUtil.setter(instance,columnParam,object,javaTypeClass);
                                    flag = false;
                                }
                            }
                        }
                    }
                resultList.add(instance);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }
}
